package edu.snake.implement.view;


import edu.snake.core.model.arena.Position;
import edu.snake.implement.ConfigConstant;
import edu.snake.implement.model.CharacterName;

import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.HashMap;
import java.util.Map;


public class UniversalRender {
    Map<CharacterName, Image> characterImageMap;


    public UniversalRender() {
        initialize();
    }

    private void initialize() {
        characterImageMap = new HashMap<CharacterName, Image>();
        characterImageMap.put(CharacterName.FROG, new ImageIcon(this.getClass().getResource("./resources/frog_1.png")).getImage());
        characterImageMap.put(CharacterName.SNAKE_HEAD, new ImageIcon(this.getClass().getResource("./resources/head.png")).getImage());
        characterImageMap.put(CharacterName.SNAKE, new ImageIcon(this.getClass().getResource("./resources/dot.png")).getImage());
        characterImageMap.put(CharacterName.SNAKE_TAIL, new ImageIcon(this.getClass().getResource("./resources/tail.png")).getImage());
//        characterImageMap.put(CharacterName.FROG_1, new ImageIcon(this.getClass().getResource("./resources/frog_2.png")).getImage());

    }

    public void render(Position position, CharacterName characterName, Graphics g) {
        Image image = characterImageMap.get(characterName);

        draw(position, g, image);
    }

    public void draw(Position position, Graphics g, Image image) {
        int cellSizePixel = ConfigConstant.cellSizePixel;
        g.fillRect(
                position.getWidth() * cellSizePixel
                , position.getHeight() * cellSizePixel
                , cellSizePixel
                , cellSizePixel
        );
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(
                position.getWidth() * cellSizePixel
                , position.getHeight() * cellSizePixel
                , cellSizePixel
                , cellSizePixel
        );
        if (image != null) {
            g.drawImage(image
                    , position.getWidth() * cellSizePixel
                    , position.getHeight() * cellSizePixel
                    , cellSizePixel
                    , cellSizePixel
                    , null);

        }
    }
}